package com.sgcc.sx.pmos.e_sx_personalcenter.presenter;

import android.text.TextUtils;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.sgcc.sx.pmos.common.base.BasePresenter;
import com.sgcc.sx.pmos.common.bean.BaseResponse;
import com.sgcc.sx.pmos.common.http.HttpInterConst;
import com.sgcc.sx.pmos.common.http.HttpRequestUtils;
import com.sgcc.sx.pmos.common.http.MvpNetCallback;
import com.sgcc.sx.pmos.e_sx_personalcenter.bean.CollectionBean;
import com.sgcc.sx.pmos.e_sx_personalcenter.bean.EstimatePowerChangeBean;
import com.sgcc.sx.pmos.e_sx_personalcenter.contract.CollectionContract;
import com.sgcc.sx.pmos.e_sx_personalcenter.contract.EstimatePowerChangeContract;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

public class EstimatePowerChangePresenter extends BasePresenter<EstimatePowerChangeContract.View> implements EstimatePowerChangeContract.Presenter {


    @Override
    public void query() {

        Map<String, Object> params = new HashMap<>();
        params.put("_t", System.currentTimeMillis());
        HttpRequestUtils.getInstance().request(params, HttpInterConst.USER_SELECTMONTHAVGANDSETTLEMENTENERGY,
                new MvpNetCallback<EstimatePowerChangeBean>(getView(), true) {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        super.onError(call, e, id);
                        // 失败回调 判断界面是否销毁
                        if (!isViewAttached()) {
                            return;
                        }
                        getView().queryFailed();
                    }

                    @Override
                    public void onResponse(EstimatePowerChangeBean response, int id) {
                        super.onResponse(response, id);
                        if (!isViewAttached()) {
                            return;
                        }
                        if (null != response) {
                            if (null != response.getData())
                                getView().querySuccess(response.getData());
                            else {
                                if (!TextUtils.isEmpty(response.getMsg())) {
                                    ToastUtils.showLong(response.getMsg());
                                }
                            }
                        }
                    }
                });

    }

    @Override
    public void update(String energy) {

        Map<String, Object> params = new HashMap<>();
        params.put("_t", System.currentTimeMillis());
        params.put("type", "1");
        params.put("energy", energy);
        HttpRequestUtils.getInstance().request(params, HttpInterConst.USER_SAVECHOOSEENERGYTYPE,
                new MvpNetCallback<BaseResponse>(getView(), true) {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        super.onError(call, e, id);
                        // 失败回调 判断界面是否销毁
                        if (!isViewAttached()) {
                            return;
                        }
                        getView().updateFailed();
                    }

                    @Override
                    public void onResponse(BaseResponse response, int id) {
                        super.onResponse(response, id);
                        if (!isViewAttached()) {
                            return;
                        }
                        if (null != response) {
                            if (response.isSuccess())
                                getView().updateSuccess();
                            else {
                                if (!TextUtils.isEmpty(response.getMsg())) {
                                    ToastUtils.showLong(response.getMsg());
                                }
                            }
                        }
                    }
                });

    }
}
